package com.xunyin.modules.meituan.rest;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xunyin.annotation.AnonymousAccess;
import com.xunyin.api.ApiResult;
import com.xunyin.modules.log.AppLog;
import com.xunyin.modules.meituan.domain.TMeituanStoremap;
import com.xunyin.modules.meituan.param.MeituanParam;
import com.xunyin.modules.meituan.service.TMeituanStoremapService;
import com.xunyin.modules.meituan.service.mapper.TMeituanStoremapMapper;
import com.xunyin.utils.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 美团门店
 * </p>
 *
 * @author admin
 * @since 2023-02-28
 */
@Slf4j
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Api(value = "美团门店", tags = "共享空间:美团门店")
public class MeituanController {

    private final TMeituanStoremapMapper tMeituanStoremapMapper;

    private final TMeituanStoremapService tMeituanStoremapService;

    /**
     * 美团回调到这，获取appAuthToken
     */
    @AnonymousAccess
    @PostMapping("/meituan/notify")
    @ApiOperation(value = "美团回调", notes = "美团回调")
    public String meituanNotify(@RequestParam Map<String, String> params) {

        String appAuthToken = params.get("appAuthToken");
        String businessId = params.get("businessId");
        String ePoiId = params.get("ePoiId");
        String timestamp = params.get("timestamp");
        String poiId = params.get("poiId");
        String poiName = params.get("poiName");

        // 更新到美团门店映射表
        TMeituanStoremap tMeituanStoremap = new TMeituanStoremap();
        tMeituanStoremap.setBusinessId(Integer.parseInt(businessId));
        tMeituanStoremap.setAppAuthToken(appAuthToken);
        tMeituanStoremap.setPoiId(Integer.parseInt(poiId));
        tMeituanStoremap.setPoiName(poiName);
        String bindTime = DateUtil.timestampToDate(Long.valueOf(timestamp));
        tMeituanStoremap.setBindTime(new Date(bindTime));
        LambdaQueryWrapper<TMeituanStoremap> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(TMeituanStoremap::getStoreId, ePoiId);
        tMeituanStoremapMapper.update(tMeituanStoremap, wrapper1);

        return "{\"data\":\"success\"}";
    }

    /**
     * 用户兑换核销验券，根据券码核销验券
     *
     * @param param
     * @return
     */
    @AppLog(value = "用户美团换核销验券", type = 1)
    //@AuthCheck
    @PostMapping("/meituan/exchangeVerifiRecharge")
    @ApiOperation(value = "用户美团换核销验券", notes = "用户美团换核销验券")
    public ApiResult<String> exchangeVerifiRecharge(@Validated @RequestBody MeituanParam param)
            throws IOException {
        int reValue = tMeituanStoremapService.exchangeVerifiRecharge(param);
        return ApiResult.ok("兑换成功");
    }

}
